<!--
    -  Copyright (c) 2018-2025, haohanwork.com All rights reserved.
  -->
<template>
    <div class="execution">
        <basic-container>
            <avue-crud ref="crud"
                       :page.sync="page"
                       :data="tableData"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @on-load="getList"
                       @search-change="searchChange"
                       @refresh-change="refreshChange">

                <template slot="menuLeft" slot-scope="scope">
                    <el-button type="primary" size="mini" icon="el-icon-plus"
                               v-show="addBtn"
                               @click="handleAdd">新增
                    </el-button>
                </template>
                <template slot="menuRight" slot-scope="scope">
                    <el-button type="primary" size="mini" icon="el-icon-view"
                               @click="platformView">平台商家
                    </el-button>
                </template>
                <template slot="menu" slot-scope="scope">
                    <el-button type="text" size="mini" icon="el-icon-view"
                               @click="handleDetail(scope.row)">详情
                    </el-button>
                    <el-button type="text" size="mini" icon="el-icon-edit"
                               v-show="editBtn && scope.row.pdsType !=='1'"
                               @click="handleEdit(scope.row)">修改
                    </el-button>
                    <el-button type="text" size="mini" icon="el-icon-delete"
                               v-show="delBtn && scope.row.pdsType !=='1'"
                               @click="handleDel(scope.row)">删除
                    </el-button>
                </template>
            </avue-crud>
        </basic-container>
    </div>
</template>

<script>
    import {delObj, fetchList} from '../../../api/manage/merchant';
    import {tableOption} from '../../../const/crud/manage/merchant';
    import {mapGetters} from 'vuex';

    export default {
        name: 'merchant',
        data() {
            return {
                tableData: [],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption,
            }
        },
        created() {
            this.getHeight();
        },
        computed: {
            ...mapGetters(['permissions']),
            addBtn() {
                return this.vaildData(this.permissions.scm_merchant_add, false)
            },
            delBtn() {
                return this.vaildData(this.permissions.scm_merchant_del, false)
            },
            editBtn() {
                return this.vaildData(this.permissions.scm_merchant_edit, false)
            },
            searchForm() {
                return this.$refs.crud.searchForm;
            }
        },
        methods: {
            getHeight() {
                let tableHeight = Math.max(window.innerHeight - 400, 300);
                this.tableOption.maxHeight = tableHeight + 'px';
            },
            getList(page, params) {
                this.tableLoading = true
                fetchList(Object.assign({
                    current: page.currentPage,
                    size: page.pageSize
                }, params)).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                })
            },
            handleDel(row) {
                this.$confirm('是否确认删除商家' + row.merchantName, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => delObj(row.merchantId)
                ).then(res => {
                    this.$message.success('删除成功');
                    this.getList(this.page, this.filterForm(this.searchForm))
                });
            },
            handleDetail(row) {
                let query = {
                    res: {
                        merchantId: row.merchantId
                    }
                };
                this.$router.push({path: '/manage/merchant_detail', query: query})
            },
            handleEdit(row) {
                let query = {
                    res: {
                        merchantId: row.merchantId
                    }
                };
                this.$router.push({path: '/manage/merchant_edit', query: query})
            },
            handleAdd() {
                let query = {res: {}};
                this.$router.push({path: '/manage/merchant_edit', query: query})
            },
            platformView() {
                this.$router.push({path: '/manage/platform_merchant'});
            },
            /**
             * 搜索回调
             */
            searchChange(form, done) {
                this.getList(this.page, form);
                done();
            },
            /**
             * 刷新回调
             */
            refreshChange() {
                this.getList(this.page, this.filterForm(this.searchForm));
            }
        }
    }
</script>

<style lang="scss" scoped>
</style>
